package com.tianci.appstore.ui.mainpage;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

import com.coocaa.x.app.framework.app.LiteAppApplication;
import com.coocaa.x.framework.utils.AsyncTask;
import com.coocaa.x.framework.utils.LogUtils;
import com.skyworth.framework.skysdk.ipc.SkyStartActivity;
import com.tianci.appstore.AppListActivity;
import com.tianci.appstore.AppListActivity.ListActivityType;
import com.tianci.appstore.R;
import com.tianci.appstore.controller.AppStoreAppListController;
import com.tianci.appstore.ui.SkinManager;
import com.tianci.appstore.ui.control.SkyMainPageController;
import com.tianci.appstore.ui.data.SkyAppBaseData;
import com.tianci.appstore.ui.data.SkyListAppData.ListType;
import com.tianci.appstore.ui.data.SkySndCgyData;
import com.tianci.appstore.ui.widget.AlwaysMarqueeTextView;
import com.tianci.appstore.ui.widget.CatchImageView;
import com.tianci.appstore.ui.widget.CustomAnimationDrawable;
import com.tianci.appstore.ui.widget.CustomAnimationDrawable.AnimationDrawableListener;
import com.tianci.appstore.ui.widget.SkyBaseItemView;
import com.tianci.appstore.ui.widget.TipToast;
import com.tianci.appstore.utils.ImageUtils;
import com.tianci.appstore.utils.SkyConstants;
import com.tianci.appstore.utils.SkyNetImageCache;

import java.util.HashMap;
import java.util.Map;

public class MainCgyBlock extends SkyBaseItemView implements AnimationDrawableListener {
    private ImageView bgImage;
    private ImageView iconImage;
    private TextView mRedPoint = null;
    private CustomAnimationDrawable mAnimationDrawable = null;
    private static int iconWidth = SkyConstants.Div(82);
    private static int iconHeight = SkyConstants.Div(77);
    private static int titleSize = SkyConstants.Dpi(35);

    public MainCgyBlock(Context context) {
        super(context);
    }

    @Override
    protected void createView() {
        // TODO Auto-generated method stub
        bgImage = new ImageView(mContext);
        SkinManager.setBackground(bgImage, R.drawable.ui_sdk_app_bg);
        this.addView(bgImage, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        iconImage = new CatchImageView(mContext);
        iconImage.setScaleType(ScaleType.CENTER_CROP);
        LayoutParams iconParams = new LayoutParams(iconWidth, iconHeight, Gravity.CENTER_HORIZONTAL | Gravity.TOP);
        iconParams.topMargin = SkyConstants.Div(36);
        this.addView(iconImage, iconParams);

        titleText = new AlwaysMarqueeTextView(mContext);
        titleText.setGravity(Gravity.CENTER);
        titleText.setSelected(false);
        titleText.setTextSize(titleSize);
        titleText.setPadding(SkyConstants.Div(20), 0, SkyConstants.Div(20), 0);
        SkinManager.setTextColor(titleText, R.color.titleColor);
        LayoutParams titleParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
        titleParams.bottomMargin = SkyConstants.Div(36);
        this.addView(titleText, titleParams);
    }

    public void setBgImage(int resId) {
        SkinManager.setBackground(bgImage, resId);
    }

    @Override
    public void update(SkyAppBaseData data) {
        // TODO Auto-generated method stub
        if (this.data != null && this.data.getIconUrl().equals(data.getIconUrl())) {
            return;
        }
        this.data = data;
        if (data.getTitle() != null)
            titleText.setText(data.getTitle());
        LogUtils.d("the snd icon url is " + data.getIconUrl());
        if (data.getIconUrl() != null && !data.getIconUrl().equals("")) {
            SkyNetImageCache.getSkyNetImageCache().downloadUrl("", data.getIconUrl(), this, 0, 0);
        } else if (((SkySndCgyData) data).getAnimIconUrl() != null)
            SkinManager.setBackground(iconImage, SkyConstants.getIconId(
                    ((SkySndCgyData) data).getAnimIconUrl().toLowerCase()));
        if (((SkySndCgyData) data).getCount() != 0) {
            addRedPoint(((SkySndCgyData) data).getCount());
        }
    }

    public void addRedPoint(int num) {
        if (mRedPoint == null) {
            mRedPoint = new TextView(mContext);
            mRedPoint.setGravity(Gravity.CENTER);
            mRedPoint.setTextColor(Color.WHITE);
            SkinManager.setBackground(mRedPoint, R.drawable.pic_redpoint);
            mRedPoint.setTextSize(SkyConstants.Dpi(32));
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    SkyConstants.Div(56), SkyConstants.Div(58), Gravity.RIGHT);
            params.setMargins(0, SkyConstants.Div(20), SkyConstants.Div(20), 0);
            addView(mRedPoint, params);
        }
        mRedPoint.setText(String.valueOf(num));
        mRedPoint.setVisibility(View.VISIBLE);
    }

    public void hiddinRedPoint() {
        if (mRedPoint != null) {
            if (mRedPoint.getVisibility() == View.VISIBLE) {
                AppStoreAppListController.getInstance().saveConfig(data.getId());
                mRedPoint.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onFocusChange(View arg0, boolean hasFocus) {
        // TODO Auto-generated method stub
        super.onFocusChange(arg0, hasFocus);
        if (hasFocus) {
            Drawable d = iconImage.getBackground();
            if (d instanceof AnimationDrawable) {
                AnimationDrawable anim = (AnimationDrawable) d;
                if (anim != null) {
                    if (anim.isRunning())
                        anim.stop();
                    anim.setOneShot(true);
                    anim.start();
                }
            }
        } else {

        }
    }

    @Override
    protected void refreshImage(String url, final Bitmap result) {
        // TODO Auto-generated method stub
        if (result != null) {
            if (result.getWidth() > 200) {
                //AnimationDrawable drawable = ImageUtils.generateAnimation(result, 13, 1);
                AsyncTask.runOnThread(new Runnable() {
                    @Override
                    public void run() {
                        mAnimationDrawable = ImageUtils.generateAnimation(result, 13, 1);
                        AsyncTask.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mAnimationDrawable.selectDrawable(0);
                                mAnimationDrawable.setAnimationDrawableListener(MainCgyBlock.this);
                                iconImage.setBackgroundDrawable(mAnimationDrawable);
                            }
                        });
                    }
                });
            } else {
                iconImage.setBackgroundDrawable(new BitmapDrawable(result));
            }
        }
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        LogUtils.i("log","the index is " + mIndex);
        LogUtils.i("detail", "cgyBlock, onClick, data = " + data.toString());
        if (!LiteAppApplication.checkNetworkConnection(TipToast.showNetIsNotConnectedToastRunnable))
            return;

        AppListActivity.type = ListActivityType.SORT_LIST;
        Intent intent = new Intent(mContext, AppListActivity.class);
        intent.putExtra("listType", ListType.CGY_APP_LIST.toString());
        intent.putExtra("cgyId", data.getId());
//        mContext.startActivity(intent);
        HashMap<String,String> map = new HashMap<String,String>();
        SkyStartActivity.startActivity(mContext, mContext.getClass().getName(), intent, map, true);
        hiddinRedPoint();
        Map<String,String> params = new HashMap<String, String>();
        params.put("category", SkyMainPageController.getCurrentCategoryData().name + "页");
        params.put("id", SkyConstants.LOG_MAP.get(mIndex));
        params.put("block_content_name", data.getTitle());
        params.put("block_content_id", data.getId());
        LogUtils.submit(LogUtils.PRODUCTID_APPSTORE, "as2_main_block_onclick", params);
        super.onClick(arg0);
    }


    @Override
    public void onAnimationEnd() {
        if (mAnimationDrawable != null) {
            mAnimationDrawable.stop();
            mAnimationDrawable.selectDrawable(0);
        }
    }

    @Override
    public void destory() {
        // TODO Auto-generated method stub
//        if (mAnimationDrawable != null)
//            mAnimationDrawable.setCallback(null);
//        mAnimationDrawable = null;
    }
}
